<?php
require_once '../minifier/jsrequire.class.php';
$require = new JSRequire();
$response = $require->fetch($_GET['script'], $_GET['sources']);

$pattern = "/Class.define\((.+)\);/";
if(preg_match_all($pattern, $response, $matches))
{
	foreach($matches[0] as $match)
	{
		// match == Class.define("something", proto).extends("something other");
		$classNamePattern = "/.define\((.+),/";
		$protoPattern = "/,(.+)\)(\.|;)/";
		if(preg_match($classNamePattern, $match, $classMatches))
		{
			$className = str_replace(".define(","",$classMatches[0]); // exclude .define(
			$className = trim(substr($className,0,strlen($className)-1)); // exclude ','
		}
		
		if(preg_match($protoPattern, $match, $protoMatches))
		{
			$classProto = $protoMatches[0];
			$classProto = trim(substr($classProto,1,strlen($classProto)-3)); // exclude , and );
			if(strpos($classProto, ".extend(") !== false)
				$classProto = substr($classProto,0,strpos($classProto, ".extend(")-1);
		}
		
		$response = str_replace($match, "Class.update($className, $classProto);", $response);
	}
}

header('Content-type: application/x-javascript');
echo $response;
?>
